<?php
namespace Home\Controller;
use Think\Controller;

/**
 * Search Related Display Controller
 *
 * @package Home\Controller
 * @author lythesia
**/
class SearchController extends Controller {
  /**
   * Display find movie/person result page
   *
   * @param  string query string(keywords)
   * @param  string query type(optional, default movie)
   * @param  string|int paging number(optional, default 1)
   * @return 
   * @author lythesia
   **/
  public function find($query, $type='movie', $p=1) {
    $query = I('get.query', '', 'trim,filter_query_str,trim');
    $type = I('get.type', 'movie', 'trim');
    $p = I('get.p', 1, 'int');

    $size = 12;

    if($type == 'movie') {
      $cond['title,aka'] = $query;
      $opt = array(
        'offset' => $size*($p-1),
        'size'   => $size,
      );
      $field = 'id,title,aka,poster,genre,releasedate';
      list($count, $data) = A('Common/Movie')->getMoviesByCond($cond, $opt, $field, true);
      $page = new \Org\Util\BootStrapPage($count, $size);

      $this->assign('search_movie', true);
      $this->assign('data', $data);
      $this->assign('count', $count);
      $this->assign('query', trim(str_replace('+', '', $query)));
      $this->assign('page', $page->show());
      $this->assign('result', 'Search/result-movie');

      $this->display('Search/result-base');
    }
    else if($type == 'person') {
      $cond['name,aka'] = $query;
      $opt = array(
        'offset' => $size*($p-1),
        'size'   => $size,
      );
      $field = 'id,name,aka,poster,occupation,birthdate,deathdate';
      list($count, $data) = A('Common/Person')->getPersonsByCond($cond, $opt, $field, true);
      $page = new \Org\Util\BootStrapPage($count, $size);

      $this->assign('search_person', true);
      $this->assign('data', $data);
      $this->assign('count', $count);
      $this->assign('query', trim(str_replace('+', '', $query)));
      $this->assign('page', $page->show());
      $this->assign('result', 'Search/result-person');

      $this->display('Search/result-base');
    }
    else {
      $this->display('Common/404');
    }
  }


  /**
   * Display findr input page
   *
   * @return 
   * @author lythesia
   **/
  public function findr() {
    $this->assign('script', 'Common/script-msry');
    $this->display('Search/advance-movie');
  }


  /**
   * Display findr result page(This page appeals to ajax display)
   *
   * @return 
   * @author lythesia
   **/
  public function findr_process() {
    $raw_cond = $_GET;
    $query_str = $_SERVER['QUERY_STRING'];

    $fmt_cond = array();
    $opt = array();
    $size = 30;

    $opt['offset'] = 0;
    $opt['size'] = $size;

    foreach($raw_cond as $key => $val) {
      if($val) {
        switch($key) {
          /* title */
        case 'title':
          $title = trim(filter_query_str(trim($val)));
          if(!check_query_len($title)) {
            $err_msg= 'Your keywords in Title are too short.';
            break 2;
          }
          $fmt_cond['title,aka'] = $title;
          break;

          /* releasedate */
        case 'releasedate-min': case 'releasedate-max':
          $date = filter_date(trim($val));
          if(!checkdate($date['mon'], $date['day'], $date['year'])) {
            $err_msg = 'Your input date is invalid.';
            break 2;
          }
          $date_new = $date['year'].'-'.$date['mon'].'-'.$date['day'];
          $fmt_cond['releasedate'][] = ($key == 'releasedate-min') ? 
            array('EGT', $date_new) : array('ELT', $date_new);
          break;

          /* rating */
        case 'rating-min': case 'rating-max':
          $rating = floatval($val);
          $fmt_cond['rating'][] = ($key == 'rating-min') ?
            array('EGT', $rating) : array('ELT', $rating);
          break;

          /* runtime */
        case 'runtime-min': case 'runtime-max':
          $runtime = intval($val);
          $fmt_cond['runtime'][] = ($key == 'runtime-min') ?
            array('EGT', $runtime) : array('ELT', $runtime);
          break;

          /* genre */
        case 'genre':
          $fmt_cond['genre'] = implode(' ', $val);
          break;

          /* mpaa */
        case 'mpaa':
          $fmt_cond['mpaa'] = array('IN', $val);
          break;

          /* country */
        case 'country':
          $fmt_cond[$key] = $val;
          break;

          /* sort */
        case 'sort':
          $opt['order'] = str_replace(',', ' ', $val);
          break;

          /* offset & size */
        case 'offset': case 'size':
          $opt[$key] = $val;
          break;
        case 'page':
          $opt['offset'] = $size*($val-1);
          break;
        }
      }
    }
    $field = 'id,title,genre,releasedate,poster';

    if(!$fmt_cond) {
      $err_msg = 'You must specify at least one field.';
    }

    if($err_msg) {
      $data['status'] = 'fail';
      $data['error']  = $err_msg;
    }
    else {
      $data['status'] = 'success';
      $data['data'] = A('Common/Movie')->getMoviesByCond($fmt_cond, $opt, $field);
      foreach($data['data'] as &$val) {
        $val['link'] = U('movie/'.$val['id']);
      }
    }

    $this->assign('data', $data);
    $this->assign('script', 'Common/script-msry');

    $this->display('Search/result-advance');
  }
}
